<?php 
/**
* 类型控制器
*/
class TypeController extends AuthController{
	private $model;

	 // 初始化函数
	public function __init(){
		$this->model = K('Type');
	}

	// 首页
	public function index(){
		$data = $this->model->all();
		$this->assign('data',$data);
		$this->display();
	}

	// 添加类型
	public function add(){
		if(IS_POST){
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加成功',U('Admin/Type/index'));
		}
		$this->display();
	}

	// 编辑
	public function edit(){
		$tid = Q('get.tid',0,'intval');
		// 获得旧数据
		$oldData = $this->model->where("tid={$tid}")->find();
		$this->assign('oldData',$oldData);
		if(IS_POST){
			$this->model->where("tid={$tid}")->update();
			$this->success('修改成功',U('index'));
		}
		$this->display();
	}

	// 删除
	public function del(){
		// 获得要删除的文章aid，单独删除
	   	$tid = Q('get.tid',0,'intval');
	   	$this->model->where("tid={$tid}")->delete();	

	   	// 批量删除
	   	// 勾选批量删除的tid
	   	$moredel = Q('post.moredel',array());
	   	foreach ($moredel as $v) {
			$this->model->where("tid={$v}")->delete();
		}   	
	   	$this->success('删除成功',U('index'));
	}

	// 属性列表
	public function attr_list(){
		$tid = Q('get.tid',0,'intval');
		$data = K('Type_attr')->where("type_tid={$tid}")->all();
		$this->assign('data',$data);
		$this->display();
	}

	// 添加属性
	public function add_attr(){
		$tid = Q('get.tid',0,'intval');
		if(IS_POST){
			if(!K('Type_attr')->addData()){
				$this->error(K('Type_attr')->error);
			}
			$this->success('添加成功',U('index'));
		}
		$this->display();
	}

	// 编辑属性
	public function edit_attr(){
		$taid = Q('get.taid',0,'intval');
		// p($taid);
		// 获得旧数据
		$oldData = K('Type_attr')->where("taid={$taid}")->find();
		// p($oldData);die;
		$this->assign('oldData',$oldData);
		if(IS_POST){
			K('Type_attr')->where("taid={$taid}")->update();
			$this->success('修改成功',U('index'));
		}
		$this->display();
	}

	// 删除属性
	public function attr_del(){
		$taid = Q('get.taid',0,'intval');
		K('Type_attr')->where("taid={$taid}")->delete();	

	   	// 批量删除
	   	// 勾选批量删除的tid
	   	$moredel = Q('post.moredel',array());
	   	foreach ($moredel as $v) {
			K('Type_attr')->where("taid={$v}")->delete();
		}   	
	   	$this->success('删除成功',U('index'));
	}




}
 ?>